var ex = require('express');
var qs = require('querystring');
var https = require('https');
var path = require('path');
var fs = require('fs');
var router = ex.Router();

//相当于初始化 车站-代号 对应表的信息
router.post('/searchTrain',function(req,res){
				//此处的代码是截取station.json中的车站信息 写成 车站-代号 对应表  注意json是双引号  否者会解析错误
				/*
				var myData = '';
				fs.readFile("station.json",'utf-8',function(err,data){  
					if(err){  
						console.log("error"+err);  
					}else{  
						//console.log("data==="+data);  
						myData = data;
						//console.log("myData==="+myData); 
						 var strs= new Array(); //定义一数组
						strs=myData.split("@"); //字符分割 
						
						var city = '';
						var cityCode = '';
						
						console.log("Array==="+strs[0]);
							
							for(var i=0;i<strs.length;i++){
								
									 var cityShuzu= new Array(); //定义一数组
										cityShuzu=strs[i].split("|"); //字符分割 
								
								
									city += cityShuzu[1]+"#";
									cityCode += cityShuzu[2]+"#";
								
								
							}
						
						console.log("Array==="+city);
						fs.writeFile('station_city1.json', city, function (err) {
							 if (err) throw err;
							 console.log('It\'s saved!'); //文件被保存
						});
						
						fs.writeFile('station_cityCode.json', cityCode, function (err) {
							 if (err) throw err;
							 console.log('It\'s saved!'); //文件被保存
						});
						
					}  
				});  
				*/
				
				
				fs.readFile("station.json",'utf-8',function(err,data){  
					if(err){  
						console.log("error"+err);  
					}else{  
							//console.log(typeof data);
							var sss = JSON.parse(data);
							//console.log(sss);
							//console.log(typeof sss);
							//console.log(sss['诏安']);
							res.json(sss);
					}		
				});			
});



//查寻火车票信息
router.post('/selectTrain',function(req,res){

			var alldata='';
			req.addListener('data',function(chunk){
				alldata+=chunk;
			});
			req.addListener('end',function(){
				var dataObj = qs.parse(alldata);
				//console.log(dataObj.date+'==========='+dataObj.start+'==========='+dataObj.end);
				// 拼装地址
				var url = 'https://kyfw.12306.cn/otn/lcxxcx/query?purpose_codes=ADULT&queryDate='+dataObj.date+'&from_station='+dataObj.start+'&to_station='+dataObj.end+'';
				//console.log("url===="+url);
				//生成GET请求的参数
				var options = {
					hostname: 'kyfw.12306.cn',
					port: 443,
					path: url,
					method: 'GET',
					rejectUnauthorized: false,
				};
				//发送get请求到气象数据开放平台
				var request = https.request(options, function (response) {
					response.setEncoding('utf8');
					var str ='';
					// 数据流动
					response.on('data', function (chunk) {
						str += chunk;
					});
					// 结束数据流动
					response.on('end', function (chunk) {
						//res.json(JSON.parse(str));
						//console.log("str=========================================="+str);
						res.json(JSON.parse(str));
					});
				});
				// 报错信息
				request.on('error', function (e) {
					console.log('ERROR: ' + e.message);
				});
				// 结束响应
				request.end();
			});
});


module.exports = router;
